package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.InvoiceInfoBean;
import com.koron.css2.baseConfig.bean.ProjectPayeeBean;
import com.koron.css2.baseConfig.mapper.InvoiceInfoMapper;
import com.koron.css2.baseConfig.mapper.ProjectPayeeMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 工程收款主体 修改
 * @author lch
 *
 */
public class ProjectPayeeUpdate implements ServerInterface{
	private static Logger log = LogManager.getLogger(ProjectPayeeSave.class);
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			ProjectPayeeBean bean = JsonUtils.objectToPojo(req.getData(), ProjectPayeeBean.class);
			ProjectPayeeMapper mapper = factory.getMapper(ProjectPayeeMapper.class);
			InvoiceInfoMapper invoiceInfoMapper = factory.getMapper(InvoiceInfoMapper.class);
			bean.setUpdateInfo(userInfo);
			mapper.update(bean);
			List<InvoiceInfoBean> invoiceInfoList = bean.getInvoiceInfos();
			invoiceInfoMapper.deleteAll(bean.getId());
			if (invoiceInfoList.size() > 0){
				for (InvoiceInfoBean invoiceInfo : invoiceInfoList){
					invoiceInfo.setCreateInfo(userInfo);
					invoiceInfo.setProjectPayeeId(bean.getId());
					invoiceInfoMapper.addInvoiceInfo(invoiceInfo);
				}
			}
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
		}catch(Exception e) {
			log.error("修改失败",e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改失败", void.class);
		}
	}

}
